#if defined(TSINGMA_MX) || defined(TSINGMA_GX) || defined(ARCTIC)
#if (FEATURE_MODE == 0)
/**
 @file sys_tmm_npm.c

 @date 2019-9-4

 @version v1.0

 The file contains TMM NPM APIs of sys layer
*/

/***************************************************************
 *
 * Header Files
 *
 ***************************************************************/
#include "sal.h"
#include "ctc_error.h"
#include "ctc_debug.h"
#include "ctc_nexthop.h"
#include "ctc_oam.h"
#include "sys_usw_common.h"
#include "sys_tmm_npm.h"
#include "sys_usw_oam_db.h"
#include "sys_usw_oam_bfd_db.h"
#include "drv_api.h"
#include "sys_usw_ftm.h"
#include "sys_usw_dma.h"
#include "sys_usw_interrupt.h"
#include "sys_usw_npm.h"
#include "sys_usw_nexthop_api.h"

#define SYS_NPM_UINT64_MAX      0xFFFFFFFFFFFFFFFFLLU
#define SYS_NPM_UINT30_MAX      0x3FFFFFFF
#define SYS_NPM_UINT22_MAX      0x3FFFFF
#define SYS_NPM_UINT16_MAX      0xFFFF
#define SYS_NPM_UINT10_MAX      0x3FF
#define SYS_NPM_NTP_MAX_TOTAL_DELAY 0x1DCD64FF8LLU
#define SYS_NPM_PTP_MAX_TOTAL_DELAY 0x7FFFFFFE0LLU
#define SYS_NPM_Z_BIT           14
#define SYS_NPM_IPV4_HEADER_LEN 20
#define SYS_NPM_IPV6_HEADER_LEN 40
#define SYS_NPM_UPD_HEADER_LEN  8
#define SYS_NPM_L2_HEADER_LEN  14



STATIC int32
_sys_tmm_npm_lite_del_from_asic(uint8 lchip, sys_npm_lite_session_t* npm_lite_session,ctc_npm_cfg_t* p_cfg)
{
    uint32 cmd          = 0;
    uint32 index        = 0;
    uint32 temp = 0;
    DsMa_m ds_ma;
    DsBfdMep_m ds_bfd_mep;
    DsBfdMep_m DsBfdMep_mask;
    DsBfdRmep_m ds_bfd_rmep;
    DsBfdRmep_m DsBfdRmep_mask;
    DsOamTwampCfg_m ds_twamp_cfg;
    DsOamTwampTxCfg_m ds_twamp_tx_cfg;
    DsOamTwampStats_m ds_twamp_stats;
    tbl_entry_t tbl_entry;

    sal_memset(&ds_ma, 0, sizeof(ds_ma));
    sal_memset(&ds_bfd_mep, 0, sizeof(ds_bfd_mep));
    sal_memset(&DsBfdMep_mask, 0, sizeof(ds_bfd_mep));
    sal_memset(&ds_bfd_rmep, 0, sizeof(ds_bfd_rmep));
    sal_memset(&DsBfdRmep_mask, 0, sizeof(ds_bfd_rmep));

    sal_memset(&ds_twamp_cfg, 0, sizeof(ds_twamp_cfg));
    sal_memset(&ds_twamp_tx_cfg, 0, sizeof(ds_twamp_tx_cfg));
    sal_memset(&ds_twamp_stats, 0, sizeof(ds_twamp_stats));


    if (npm_lite_session->is_ipv6 && !p_cfg->lite_cfg.enable)
    {
        index = npm_lite_session->mep_idx;
        cmd = DRV_IOR(DsBfdMep_t, DsBfdMep_ipDaL_f);
        DRV_FIELD_IOCTL(lchip, index, DRV_CMD_PP_EN(cmd), &temp);
        CTC_ERROR_DUMP(_sys_usw_bfd_free_ipv6_idx(lchip, temp));

        index = npm_lite_session->mep_idx + 1;
        cmd = DRV_IOR(DsBfdRmep_t, DsBfdRmep_ipSa_f);
        DRV_FIELD_IOCTL(lchip, index, DRV_CMD_PP_EN(cmd), &temp);
        CTC_ERROR_DUMP(_sys_usw_bfd_free_ipv6_idx(lchip, temp));
    }

    index = npm_lite_session->ma_idx;
    CTC_ERROR_DUMP(sys_usw_ma_add_to_asic(lchip, index, &ds_ma));

    index = npm_lite_session->mep_idx;
    cmd = DRV_IOW(DsBfdMep_t, DRV_ENTRY_FLAG);
    tbl_entry.data_entry = (uint32*)&ds_bfd_mep;
    tbl_entry.mask_entry = (uint32*)&DsBfdMep_mask;
    DRV_IOCTL(lchip, index, DRV_CMD_PP_EN(cmd), &tbl_entry);

    index = npm_lite_session->mep_idx+1;
    cmd = DRV_IOW(DsBfdRmep_t, DRV_ENTRY_FLAG);
    tbl_entry.data_entry = (uint32*)&ds_bfd_rmep;
    tbl_entry.mask_entry = (uint32*)&DsBfdRmep_mask;
    DRV_IOCTL(lchip, index, DRV_CMD_PP_EN(cmd), &tbl_entry);

    index = npm_lite_session->twamp_idx;
    cmd = DRV_IOW(DsOamTwampCfg_t, DRV_ENTRY_FLAG);
    DRV_IOCTL(lchip, index, DRV_CMD_PP_EN(cmd), &ds_twamp_cfg);

    cmd = DRV_IOW(DsOamTwampTxCfg_t, DRV_ENTRY_FLAG);
    DRV_IOCTL(lchip, index, DRV_CMD_PP_EN(cmd), &ds_twamp_tx_cfg);

    cmd = DRV_IOW(DsOamTwampStats_t, DRV_ENTRY_FLAG);
    DRV_IOCTL(lchip, index, DRV_CMD_PP_EN(cmd), &ds_twamp_stats);

    return CTC_E_NONE;
}


STATIC int32
_sys_tmm_npm_lite_add_to_asic(uint8 lchip, ctc_npm_cfg_t* p_cfg, sys_npm_lite_session_t* npm_lite_session)
{
    uint32 cmd        = 0;
    uint32 index      = 0;
    uint8  ipv6_sa_index = 0;
    uint8  ipv6_da_index = 0;
    uint32 ipDa_h = 0;
    uint32 ipDa_l = 0;
    uint32 value = 0;
    uint8 is_twamp = 0;
    uint8 is_ntp_mode = 0;
    uint32 rate = 0;
    uint16 lport = 0;
    uint8 gchip_id = 0;
    sys_oam_nhop_info_t oam_nhop_info;
    DsMa_m ds_ma;
    DsBfdMep_m ds_bfd_mep;
    DsBfdMep_m DsBfdMep_mask;
    DsBfdRmep_m ds_bfd_rmep;
    DsBfdRmep_m DsBfdRmep_mask;
    DsOamTwampCfg_m ds_twamp_cfg;
    DsOamTwampTxCfg_m ds_twamp_tx_cfg;
	DsOamTwampStats_m ds_twamp_stats;
    tbl_entry_t  tbl_entry;
    int32 ret = CTC_E_NONE;

    sal_memset(&ds_ma, 0, sizeof(DsMa_m));
    sal_memset(&ds_bfd_mep, 0, sizeof(DsBfdMep_m));
    sal_memset(&DsBfdMep_mask, 0, sizeof(DsBfdMep_m));
    sal_memset(&ds_bfd_rmep, 0, sizeof(DsBfdRmep_m));
    sal_memset(&DsBfdRmep_mask, 0, sizeof(DsBfdRmep_m));
    sal_memset(&oam_nhop_info, 0, sizeof(oam_nhop_info));
    sal_memset(&ds_twamp_cfg, 0, sizeof(DsOamTwampCfg_m));
    sal_memset(&ds_twamp_tx_cfg, 0, sizeof(DsOamTwampTxCfg_m));

    is_twamp = CTC_FLAG_ISSET(p_cfg->flag, CTC_NPM_CFG_FLAG_TWAMP_LITE);
    is_ntp_mode = CTC_FLAG_ISSET(p_cfg->flag, CTC_NPM_CFG_FLAG_NTP_TS);
    cmd = DRV_IOR(DsOamTwampTxCfg_t, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, npm_lite_session->twamp_idx, DRV_CMD_PP_EN(cmd), &ds_twamp_tx_cfg));

    rate = p_cfg->rate;
    rate = rate? rate : 1;
    SetDsOamTwampTxCfg(V, periodIntervalCfg_f                  , &ds_twamp_tx_cfg , rate);
    SetDsOamTwampTxCfg(V, periodWhile_f                  , &ds_twamp_tx_cfg , rate);
    if ((p_cfg->lite_cfg.mode == CTC_NPM_SESSION_LITE_MODE_SENDER)
        && (p_cfg->tx_mode == CTC_NPM_TX_MODE_PACKET_NUM))
    {
        SetDsOamTwampTxCfg(V, txSeqNum_f                  , &ds_twamp_tx_cfg , 0);
        SetDsOamTwampTxCfg(V, stopOnTxPktNum_f                  , &ds_twamp_tx_cfg , p_cfg->packet_num-1);
    }
    if (!is_twamp)
    {
        SetDsOamTwampTxCfg(V, txDmType_f                  , &ds_twamp_tx_cfg , p_cfg->lite_cfg.dm_tx_type);
    }

    cmd = DRV_IOW(DsOamTwampTxCfg_t, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, npm_lite_session->twamp_idx, DRV_CMD_PP_EN(cmd), &ds_twamp_tx_cfg));

    cmd = DRV_IOR(DsOamTwampCfg_t, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, npm_lite_session->twamp_idx, DRV_CMD_PP_EN(cmd), &ds_twamp_cfg));

    if (CTC_NPM_MEASURE_MODE_FAR_END == p_cfg->dm_stats_mode)
    {
        value = 0;
    }
    else if (CTC_NPM_MEASURE_MODE_NEAR_END == p_cfg->dm_stats_mode)
    {
        value = 1;
    }
    else if(CTC_NPM_MEASURE_MODE_TWO_WAY == p_cfg->dm_stats_mode)
    {
        value = 2;
    }
    SetDsOamTwampCfg(V, measureType_f                  , &ds_twamp_cfg , value);
    if (is_twamp)
    {
        value = (p_cfg->lite_cfg.mode == CTC_NPM_SESSION_LITE_MODE_REFLECTOR_LIGHT)? 1 : 0;
        SetDsOamTwampCfg(V, u_gOther_twampLightEn_f                  , &ds_twamp_cfg , value);
    }
    if (p_cfg->lite_cfg.nhid)
    {
        CTC_ERROR_RETURN(_sys_usw_oam_get_nexthop_info(lchip, p_cfg->lite_cfg.nhid, 0, &oam_nhop_info));
    }
    if (p_cfg->lite_cfg.mode != CTC_NPM_SESSION_LITE_MODE_SENDER && CTC_FLAG_ISSET(p_cfg->flag, CTC_NPM_CFG_FLAG_OFFSET_ADJ))
    {
        SetDsOamTwampCfg(V, packetOffsetAdjust_f         , &ds_twamp_cfg , TRUE);
        SetDsOamTwampCfg(V, packetOffset_f  , &ds_twamp_cfg , (p_cfg->lite_cfg.is_ipv6? 
            SYS_NPM_IPV6_HEADER_LEN: SYS_NPM_IPV4_HEADER_LEN) + SYS_NPM_UPD_HEADER_LEN
            +(oam_nhop_info.swap_mac?SYS_NPM_L2_HEADER_LEN:0));
    }
    
    cmd = DRV_IOW(DsOamTwampCfg_t, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, npm_lite_session->twamp_idx, DRV_CMD_PP_EN(cmd), &ds_twamp_cfg));
    if (!is_twamp)
    {
        return CTC_E_NONE;
    }
    SetDsBfdMep(V, twampIndex_f                  , &ds_bfd_mep , npm_lite_session->twamp_idx);
    SetDsBfdMep(V, twampIndex_f                  , &DsBfdMep_mask ,  0);
    SetDsBfdMep(V, mepType_f                  , &ds_bfd_mep , DRV_ENUM(DRV_OAM_MEPTYPE_TWAMP_MEP));
    SetDsBfdMep(V, mepType_f                  , &DsBfdMep_mask ,  0);
    SetDsMa(V, updDmAfterLoop_f, &ds_ma, 1);
    SetDsMa(V, txTwampTsType_f, &ds_ma, is_ntp_mode);
    SetDsMa(V, mdLvl_f, &ds_ma, 1);
	SetDsMa(V, priorityIndex_f, &ds_ma, p_cfg->lite_cfg.tx_cos_exp);
    npm_lite_session->ts_format = is_ntp_mode;
    SetDsBfdMep(V, twampAck_f                  , &ds_bfd_mep , (p_cfg->lite_cfg.mode == CTC_NPM_SESSION_LITE_MODE_SENDER)? 1 : 0);
    SetDsBfdMep(V, twampAck_f                  , &DsBfdMep_mask ,  0);
    SetDsBfdMep(V, twampPaddingLen_f                  , &ds_bfd_mep , p_cfg->pkt_format.frame_size? (p_cfg->pkt_format.frame_size - SYS_USW_MIN_TWAMP_LEN) : 27);
    SetDsBfdMep(V, twampPaddingLen_f                  , &DsBfdMep_mask ,  0);

    SetDsMa(V, txWithSendId_f, &ds_ma, 1);    /* UPD header with BFD PDU */
    SetDsMa(V, rxOamType_f, &ds_ma, 2);

    if (p_cfg->lite_cfg.is_ipv6)
    {
        npm_lite_session->is_ipv6 = 1;
        SetDsMa(V, ipv6Hdr_f, &ds_ma, 1);  /* IPv6 header with BFD PDU */
        SetDsMa(V, packetType_f, &ds_ma, oam_nhop_info.swap_mac? 0 : 3);      /* pkt_type 0x86DD */
    }
    else
    {
        SetDsMa(V, txWithPortStatus_f, &ds_ma, 1);  /* IPv4 header with BFD PDU */
        SetDsMa(V, packetType_f, &ds_ma, oam_nhop_info.swap_mac? 0 : 1);      /* pkt_type 0x0800 */
    }
    SetDsBfdMep(V, ccmInterval_f, &ds_bfd_mep, 1);

    if (p_cfg->lite_cfg.nhid)
    {
        CTC_ERROR_RETURN(_sys_usw_oam_get_nexthop_info(lchip, p_cfg->lite_cfg.nhid, 0, &oam_nhop_info));
        SetDsMa(V, nextHopExt_f, &ds_ma, oam_nhop_info.nexthop_is_8w);
        SetDsMa(V, nextHopPtr_f, &ds_ma, oam_nhop_info.dsnh_offset);
        SetDsMa(V, ingressEdit_f, &ds_ma, !oam_nhop_info.bypass_igr_edit);
        SetDsBfdMep(V, destMap_f, &ds_bfd_mep, oam_nhop_info.dest_map);
        ret = sys_usw_nh_set_ts_edit_en(lchip, p_cfg->lite_cfg.nhid, TRUE);
        if (CTC_E_NONE == ret)
        {
            SetDsMa(V, updDmAfterLoop_f, &ds_ma, 0);
        }
    }

    if (0 == p_cfg->lite_cfg.nhid || (CTC_FLAG_ISSET(p_cfg->flag, CTC_NPM_CFG_FLAG_ILOOP)))
    {
        CTC_ERROR_RETURN(sys_usw_get_gchip_id(lchip, &gchip_id));
        lport = (0 == p_cfg->lite_cfg.nhid)? SYS_RSV_PORT_IP_TUNNEL : SYS_MAP_CTC_GPORT_TO_DRV_LPORT(p_cfg->dest_gport);
        SetDsMa(V, txForceNotBypassIpe_f, &ds_ma, 1);
        SetDsBfdMep(V, destMap_f, &ds_bfd_mep, SYS_ENCODE_DESTMAP(gchip_id, lport));
        SetDsMa(V, bfdWithVrf_f, &ds_ma, 1);
        if (CTC_FLAG_ISSET(p_cfg->flag, CTC_NPM_CFG_FLAG_VRF_EN))
        {
            SetDsMa(V, vrfid_f, &ds_ma, p_cfg->vrf_id);
        }
    }
    else
    {
        SetDsMa(V, txForceNotBypassIpe_f, &ds_ma, 0);
        if (CTC_FLAG_ISSET(p_cfg->flag, CTC_NPM_CFG_FLAG_VRF_EN))
        {
            SetDsMa(V, bfdWithVrf_f, &ds_ma, 1);
            if (CTC_NPM_SESSION_LITE_MODE_SENDER == p_cfg->lite_cfg.mode)
            {
                SetDsMa(V, vrfid_f, &ds_ma, p_cfg->vrf_id);
                SetDsBfdMep(V, destMap_f, &ds_bfd_mep, oam_nhop_info.dsnh_offset);
            }
        }
    }
    
    SetDsBfdMep(V, active_f, &ds_bfd_mep, 1);
    SetDsBfdMep(V, maIndex_f, &ds_bfd_mep, npm_lite_session->ma_idx);


    SetDsBfdMep(V, bfdSrcport_f, &ds_bfd_mep, p_cfg->lite_cfg.src_port);
    SetDsBfdMep(V, dstPort_f, &ds_bfd_mep , p_cfg->lite_cfg.dst_port);


    SetDsBfdMep(V, isBfd_f, &ds_bfd_mep, 1);
    SetDsBfdMep(V, isRemote_f, &ds_bfd_mep, 0);
    SetDsBfdMep(V, isUp_f, &ds_bfd_mep, 0);
    SetDsBfdMep(V, ethOamP2PMode_f, &ds_bfd_mep, 1);
    SetDsBfdMep(V, cciEn_f, &ds_bfd_mep, 0);

    SetDsBfdRmep(V, isRemote_f, &ds_bfd_rmep, 1);
    SetDsBfdRmep(V, isBfd_f, &ds_bfd_rmep, 1);
    /* 
    bit 14 is z bit use for distinguish timestamp format 
    0: NTP     1: PTP
    */
    SetDsBfdRmep(V, senderErrorEst_f, &ds_bfd_rmep, (((!is_ntp_mode) << SYS_NPM_Z_BIT) | 1));
    SetDsBfdRmep(V, ipBfdTtl_f , &ds_bfd_rmep, 255);

    if (p_cfg->lite_cfg.is_ipv6)
    {
        CTC_ERROR_RETURN(_sys_usw_bfd_alloc_ipv6_idx(lchip, (void*)&(p_cfg->lite_cfg.ipv6_sa), &ipv6_sa_index));
        SetDsBfdRmep(V, ipSa_f, &ds_bfd_rmep, ipv6_sa_index);
        ret = _sys_usw_bfd_alloc_ipv6_idx(lchip, (void*)&(p_cfg->lite_cfg.ipv6_da), &ipv6_da_index);
        if (ret == CTC_E_NO_RESOURCE)
        {
            CTC_ERROR_DUMP(_sys_usw_bfd_free_ipv6_idx(lchip, ipv6_sa_index));
            return ret;
        }
        ipDa_l = ipv6_da_index;
    }
    else
    {
        /* edit ip_sa*/
        SetDsBfdRmep(V, ipSa_f, &ds_bfd_rmep, p_cfg->lite_cfg.ip4_sa);
        /* edit ip_da*/
        ipDa_h = ((p_cfg->lite_cfg.ip4_da) >> 20)&0xfff;
        SetDsBfdRmep(V, ipDaH_f, &ds_bfd_rmep, ipDa_h);
        ipDa_l = (p_cfg->lite_cfg.ip4_da)&0xfffff;
    }
    SetDsBfdMep(V, ipDaL_f, &ds_bfd_mep, ipDa_l);
    SetDsBfdMep(V, ipDaL_f, &DsBfdMep_mask, 0);

    index = npm_lite_session->ma_idx;
    CTC_ERROR_GOTO(sys_usw_ma_add_to_asic(lchip, index, &ds_ma),ret,err);

    index = npm_lite_session->mep_idx;
    cmd = DRV_IOW(DsBfdMep_t, DRV_ENTRY_FLAG);
    tbl_entry.data_entry = (uint32*)&ds_bfd_mep;
    tbl_entry.mask_entry = (uint32*)&DsBfdMep_mask;
    CTC_ERROR_GOTO(DRV_IOCTL(lchip, index, DRV_CMD_PP_EN(cmd), &tbl_entry),ret,err);

    index = npm_lite_session->mep_idx + 1;
    cmd = DRV_IOW(DsBfdRmep_t, DRV_ENTRY_FLAG);
    tbl_entry.data_entry = (uint32*)&ds_bfd_rmep;
    tbl_entry.mask_entry = (uint32*)&DsBfdRmep_mask;
    CTC_ERROR_GOTO(DRV_IOCTL(lchip, index, DRV_CMD_PP_EN(cmd), &tbl_entry),ret,err);
	
    sal_memset(&ds_twamp_stats, 0, sizeof(ds_twamp_stats));
    cmd = DRV_IOW(DsOamTwampStats_t, DRV_ENTRY_FLAG);
    SetDsOamTwampStats(V, statsInit_f, &ds_twamp_stats, 1);
    CTC_ERROR_GOTO(DRV_IOCTL(lchip, npm_lite_session->twamp_idx, DRV_CMD_PP_EN(cmd), &ds_twamp_stats), ret, err);
    
    return ret;
   
err:

    if (npm_lite_session->is_ipv6)
    {
        CTC_ERROR_RETURN(_sys_usw_bfd_free_ipv6_idx(lchip, ipv6_da_index));
        CTC_ERROR_RETURN(_sys_usw_bfd_free_ipv6_idx(lchip, ipv6_sa_index));
    }

    return ret;
}

 int32
sys_tmm_npm_set_lite_config(uint8 lchip, void* p_lite_cfg)
{
    int32 ret = 0;
    sys_usw_opf_t opf;
    uint32 block_size = 0;
    uint32 offset = 0;
    sys_npm_lite_session_t* npm_lite_session = NULL;
    ctc_npm_cfg_t* p_cfg = NULL;
    uint32 session_id =0;
    uint8 mep_index_alloc_by_sdk=0;
    uint32 max_mep_index = 0;
    
    sal_memset(&opf, 0, sizeof(sys_usw_opf_t));
    p_cfg = (ctc_npm_cfg_t*)p_lite_cfg;
    CTC_MAX_VALUE_CHECK(p_cfg->lite_cfg.mode, 2);
    CTC_MAX_VALUE_CHECK(p_cfg->lite_cfg.dm_tx_type, 1);
    CTC_MAX_VALUE_CHECK(p_cfg->vrf_id, MCHIP_CAP(SYS_CAP_SPEC_MAX_VRFID));
    CTC_MAX_VALUE_CHECK(p_cfg->rate, SYS_NPM_UINT10_MAX);
    if ((p_cfg->lite_cfg.mode == CTC_NPM_SESSION_LITE_MODE_SENDER)
        && (p_cfg->tx_mode == CTC_NPM_TX_MODE_PACKET_NUM) && (DRV_IS_TMM(lchip) || DRV_IS_TMG(lchip)))
    {
        return CTC_E_NOT_SUPPORT;
    }
    if ((p_cfg->lite_cfg.mode == CTC_NPM_SESSION_LITE_MODE_SENDER)
        && (p_cfg->tx_mode == CTC_NPM_TX_MODE_PACKET_NUM))
    {
        CTC_MIN_VALUE_CHECK(p_cfg->packet_num, 2);
    }
    CTC_COS_RANGE_CHECK(p_cfg->lite_cfg.tx_cos_exp);
    if (p_cfg->pkt_format.frame_size)
    {
        CTC_MIN_VALUE_CHECK(p_cfg->pkt_format.frame_size,  SYS_USW_MIN_TWAMP_LEN);
        CTC_MAX_VALUE_CHECK(p_cfg->pkt_format.frame_size,  MCHIP_CAP(SYS_CAP_PORT_MAX_FRAMESIZE_MAX_VALUE));
    }
    session_id = SYS_NPM_LITE_SESSION_CONVERT(p_cfg->session_id);
    if ((!CTC_NPM_IS_LITE_SESSION(p_cfg->session_id))||session_id>=CTC_NPM_LITE_SESSION_NUM)
    {
        return CTC_E_INVALID_CONFIG;
    }
    SYS_CB(SYS_CB_OAM_GET_MEP_INDEX_TYPE, lchip, &mep_index_alloc_by_sdk);

    npm_lite_session = (sys_npm_lite_session_t*)ctc_vector_get(g_npm_master[lchip]->session_lite, session_id);
    if (!mep_index_alloc_by_sdk)
    {
        sys_usw_ftm_query_table_entry_num(lchip, DsEthMep_t,  &max_mep_index);
        if (max_mep_index && ((p_cfg->lite_cfg.lmep_index < SYS_OAM_MIN_MEP_INDEX)
            || (p_cfg->lite_cfg.lmep_index >= max_mep_index - 1)))
        {
            SYS_NPM_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "  Oam mep index is invalid  \n");
            return CTC_E_INVALID_PARAM;
        }
        if (!npm_lite_session)
        {
            CTC_ERROR_RETURN(sys_usw_oam_get_free_index(lchip, p_cfg->lite_cfg.lmep_index));
        }
    }

    if (NULL == npm_lite_session)
    {
        if (p_cfg->lite_cfg.enable)
        {
            npm_lite_session = (sys_npm_lite_session_t*)mem_malloc(MEM_NPM_MODULE, sizeof(sys_npm_lite_session_t));
            if (npm_lite_session)
            {
                sal_memset(npm_lite_session,0,sizeof(sys_npm_lite_session_t));
                npm_lite_session->session_id = session_id;
                npm_lite_session->mode = p_cfg->lite_cfg.mode;
                npm_lite_session->is_twamp = CTC_FLAG_ISSET(p_cfg->flag, CTC_NPM_CFG_FLAG_TWAMP_LITE);
                npm_lite_session->session_stats.min_delay = SYS_NPM_UINT64_MAX;
                npm_lite_session->lite_stats.two_way_min_jitter = CTC_MAX_UINT32_VALUE;
                npm_lite_session->lite_stats.far_min_jitter = CTC_MAX_UINT32_VALUE;
                npm_lite_session->lite_stats.near_min_jitter = CTC_MAX_UINT32_VALUE;
            }
            else
            {
                SYS_NPM_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "Twamp session: build node fail!!\n");
                return CTC_E_NO_MEMORY;
            }

            if (ctc_vector_get(g_npm_master[lchip]->session_lite, npm_lite_session->session_id))
            {
                ret = CTC_E_EXIST;
                goto error1;
            }
            if (FALSE == ctc_vector_add(g_npm_master[lchip]->session_lite, npm_lite_session->session_id, npm_lite_session))
            {
                ret =  CTC_E_NO_MEMORY;
                goto error1;
            }

            if (!g_npm_master[lchip]->session->used_cnt && 1 == g_npm_master[lchip]->session_lite->used_cnt)
            {
                 CTC_ERROR_GOTO(sys_usw_dma_set_npm_stats_enable(lchip, TRUE), ret, error2);
            }
            
            block_size = 1;
            opf.pool_type = g_npm_master[lchip]->npm_opf_type;
            opf.pool_index  = SYS_NPM_OPF_TYPE_DMM_TWAMP_LITE;
            ret = sys_usw_opf_alloc_offset(lchip, &opf, block_size, &offset);
            if (CTC_E_NONE != ret)
            {
                goto error3;
            }
            npm_lite_session->twamp_idx = offset;

            if (CTC_FLAG_ISSET(p_cfg->flag, CTC_NPM_CFG_FLAG_TWAMP_LITE))
            {
                if (!mep_index_alloc_by_sdk)
                {
                    npm_lite_session->mep_idx = p_cfg->lite_cfg.lmep_index;
                }
                ret = sys_usw_oam_npm_lite_build_index(lchip, npm_lite_session, &npm_lite_session->ma_idx);
                if (CTC_E_NONE != ret)
                {
                    SYS_NPM_DBG_OUT(CTC_DEBUG_LEVEL_ERROR, "build index fail!!\n");
                    goto error4;
                }
                p_cfg->lite_cfg.lmep_index = npm_lite_session->mep_idx; /*copy mep index*/
            }
        }
        else
        {
            return CTC_E_NOT_EXIST;
        }
    }
    else
    {
        if (!mep_index_alloc_by_sdk && npm_lite_session->mep_idx!= p_cfg->lite_cfg.lmep_index)
        {
            return CTC_E_INVALID_PARAM;
        }
        npm_lite_session->mode = p_cfg->lite_cfg.mode;
        p_cfg->lite_cfg.lmep_index = npm_lite_session->mep_idx;
    }
    if (p_cfg->lite_cfg.enable)
    {
        ret = _sys_tmm_npm_lite_add_to_asic(lchip, p_cfg, npm_lite_session);
    }
    if (p_cfg->lite_cfg.enable && !ret)
    {
        return ret;
    }
    else
    {
        goto error5;
    }
    error5:
    _sys_tmm_npm_lite_del_from_asic(lchip, npm_lite_session, p_cfg);
    if (CTC_FLAG_ISSET(p_cfg->flag, CTC_NPM_CFG_FLAG_TWAMP_LITE))
    {
        sys_usw_oam_npm_lite_free_index(lchip, &npm_lite_session->mep_idx, &npm_lite_session->ma_idx);
    }
    error4:
    offset = npm_lite_session->twamp_idx;
    block_size = 1;
    opf.pool_index  = SYS_NPM_OPF_TYPE_DMM_TWAMP_LITE;
    opf.pool_type = g_npm_master[lchip]->npm_opf_type;
    sys_usw_opf_free_offset(lchip, &opf, block_size, offset);
    error3:
    if (!g_npm_master[lchip]->session->used_cnt && 1 == g_npm_master[lchip]->session_lite->used_cnt)
    {
        sys_usw_dma_set_npm_stats_enable(lchip, FALSE);
    }
    error2:
    if (!ctc_vector_get(g_npm_master[lchip]->session_lite, session_id))
    {
        return CTC_E_NOT_EXIST;
    }
    ctc_vector_del(g_npm_master[lchip]->session_lite, session_id);
    error1:
    mem_free(npm_lite_session);
    return ret;
}

 int32
sys_tmm_npm_get_lite_stats(uint8 lchip, uint32 session_id, void* p_lite_stats)
{
    uint32 cmd =0;
    DsOamTwampTxCfg_m ds_twamp_tx_cfg;
    sys_npm_lite_session_t* npm_lite_session = NULL;
    ctc_npm_stats_t* p_stats =NULL;
    p_stats = (ctc_npm_stats_t*)p_lite_stats;
    
    sal_memset(&ds_twamp_tx_cfg, 0, sizeof(DsOamTwampTxCfg_m));
    npm_lite_session = (sys_npm_lite_session_t*)ctc_vector_get(g_npm_master[lchip]->session_lite, SYS_NPM_LITE_SESSION_CONVERT(session_id));
    if (NULL == npm_lite_session)
    {
        return CTC_E_NOT_EXIST;
    }
    if (MCHIP_NPM(lchip)->get_stats_from_asic)
    {
        MCHIP_NPM(lchip)->get_stats_from_asic(lchip,npm_lite_session);
    }
    cmd = DRV_IOR(DsOamTwampTxCfg_t, DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, npm_lite_session->twamp_idx, DRV_CMD_PP_EN(cmd), &ds_twamp_tx_cfg));
    p_stats->tx_en = GetDsOamTwampTxCfg(V, periodTestEn_f, &ds_twamp_tx_cfg);
    p_stats->max_delay = npm_lite_session->session_stats.max_delay;
    p_stats->min_delay = npm_lite_session->session_stats.min_delay;
    p_stats->min_delay = (SYS_NPM_UINT64_MAX == p_stats->min_delay) ? 0 : p_stats->min_delay;
    p_stats->total_delay = npm_lite_session->session_stats.total_delay_all;
    p_stats->disorder_pkts = npm_lite_session->session_stats.disorder_pkts;
    p_stats->rx_pkts = npm_lite_session->session_stats.rx_pkts_all;
    p_stats->rx_fcl = npm_lite_session->lite_stats.rx_fcl_all;
    p_stats->tx_pkts = npm_lite_session->session_stats.tx_pkts_all;

    p_stats->max_jitter = npm_lite_session->lite_stats.two_way_max_jitter;
    p_stats->near_end_jitter[0] = npm_lite_session->lite_stats.near_max_jitter;
    p_stats->far_end_jitter[0] = npm_lite_session->lite_stats.far_max_jitter;
    p_stats->min_jitter = npm_lite_session->lite_stats.two_way_min_jitter;
    p_stats->min_jitter = (CTC_MAX_UINT32_VALUE == p_stats->min_jitter) ? 0 : p_stats->min_jitter;
    p_stats->near_end_jitter[1] = npm_lite_session->lite_stats.near_min_jitter;
    p_stats->near_end_jitter[1] = (CTC_MAX_UINT32_VALUE == p_stats->near_end_jitter[1]) ? 0 : p_stats->near_end_jitter[1];
    p_stats->far_end_jitter[1] = npm_lite_session->lite_stats.far_min_jitter;
    p_stats->far_end_jitter[1] = (CTC_MAX_UINT32_VALUE == p_stats->far_end_jitter[1]) ? 0 : p_stats->far_end_jitter[1];
    p_stats->total_jitter = npm_lite_session->lite_stats.two_way_total_jitter_all;
    p_stats->near_end_jitter[2] = npm_lite_session->lite_stats.near_total_jitter_all;
    p_stats->far_end_jitter[2] = npm_lite_session->lite_stats.far_total_jitter_all;
    if (p_stats->clear_en)
    {
        sal_memset(&npm_lite_session->session_stats, 0, sizeof(sys_npm_common_stats_t));
        sal_memset(&npm_lite_session->lite_stats, 0, sizeof(sys_npm_lite_stats_t));
        npm_lite_session->session_stats.min_delay = SYS_NPM_UINT64_MAX;
        npm_lite_session->lite_stats.two_way_min_jitter = CTC_MAX_UINT32_VALUE;
        npm_lite_session->lite_stats.far_min_jitter = CTC_MAX_UINT32_VALUE;
        npm_lite_session->lite_stats.near_min_jitter = CTC_MAX_UINT32_VALUE;
    }

    return CTC_E_NONE;
}

int32
sys_tmm_npm_process_isr(uint8 gchip, void* p_data)
{
    ctc_npm_event_t* p_event = NULL;
    uint8 lchip = 0;
    uint8 i=0;

    CTC_PTR_VALID_CHECK(p_data);
    CTC_ERROR_RETURN(sys_usw_get_local_chip_id(gchip, &lchip));
    SYS_LCHIP_CHECK_ACTIVE(lchip);
    p_event = (ctc_npm_event_t*)p_data;
    for (i = 0; i < p_event->valid_num; i++)
    {
        SYS_NPM_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "---------------NPM Event---------------\n");
        SYS_NPM_DBG_OUT(CTC_DEBUG_LEVEL_DUMP, "Session ID: %-10d\nIn available period:%-10d\n Max ir: %-10"PRIu64"\n Min ir: %-10"PRIu64"\n Second counts: %-10"PRIu64"\n\
 Total avaiable period : %-10"PRIu64"\n Total unavaiable period : %-10"PRIu64"\n",
                        p_event->event_entry[i].session_id, p_event->event_entry[i].in_avl_period,
                        p_event->event_entry[i].ir[0],p_event->event_entry[i].ir[1],
                        p_event->event_entry[i].seconds,p_event->event_entry[i].avl_periods[0],p_event->event_entry[i].avl_periods[1]);
    }
     return CTC_E_NONE;
}

int32
sys_tmm_npm_sync_dma_stats(uint8 lchip, void* p_data)
{
    int32 ret =CTC_E_NONE;
    uint32 session_id =0;
    uint32* p_addr =NULL;
    uint32* p_addr_data =NULL;
    uint32 type =0;
    uint8 is_ntp_mode =0;
    uint64 total_delay =0;
    uint64 max_total_delay = 0;
    uint64 total_far_delay =0;
    uint64 min_delay=0;
    uint32 max_jitter =0;
    uint32 total_jitter =0;
    uint32 min_jitter =0;
    uint64 max_delay =0;
    sys_npm_lite_session_t* npm_lite_session = NULL;
    uint64 two_way_min_jitter = 0;
    uint64 far_min_jitter = 0;
    uint64 near_min_jitter = 0;
    uint64 rx_pkts_all = 0;
    uint64 rx_fcl_all = 0;
    uint64 tx_pkts_all = 0;
    uint64 disorder_pkts = 0;
    uint32 two_way_total_jitter_all = 0;
    uint32 near_total_jitter_all = 0;
    uint32 far_total_jitter_all = 0;
    uint8 lost_rate =0;
    uint32 tmp[2]={0};
    uint8 is_ses=0;
    uint64 ir=0;
    uint8 gchip=0;
    uint32 dword = 0;
    sys_npm_session_t * npm_session = NULL;
    ctc_npm_event_t npm_event;
    CTC_INTERRUPT_EVENT_FUNC npm_cb = NULL;
    AutoGenPktEn_m autogen_en;
    sys_dma_reg_t* p_dma_reg = (sys_dma_reg_t*)p_data;
    sys_dma_npm_data_t* p_ext_data = NULL;
    uint32 cmd = 0;
    uint8 auto_en = 0;
    uint8 is_first = 0;
    uint64 first_ts = 0;
    uint64 time_stamp = 0;
    
    CTC_PTR_VALID_CHECK(p_data);
    SYS_NPM_INIT_CHECK(lchip);
    p_ext_data = (sys_dma_npm_data_t*)p_dma_reg->p_ext;
    p_addr = p_dma_reg->p_data;
    type = p_ext_data->type;

    CTC_ERROR_RETURN(sys_usw_get_gchip_id(lchip, &gchip));
    CTC_ERROR_RETURN(sys_usw_interrupt_get_event_cb(lchip, CTC_EVENT_NPM, &npm_cb));
    if (!npm_cb)
    {
        return CTC_E_NOT_INIT;
    }
    NPM_LOCK(lchip);
    sal_memset(&npm_event, 0, sizeof(ctc_npm_event_t));

    if (3 == type)
    {
        goto NPM_LITE_SESSION_PROC;
    }
    for (session_id = 0; session_id < MCHIP_CAP(SYS_CAP_NPM_SESSION_NUM) ; session_id++)
    {   
        if (!g_npm_master[lchip]->session->used_cnt)
        {
            break;
        }
        npm_session = (sys_npm_session_t*)ctc_vector_get(g_npm_master[lchip]->session, session_id);
        if (!npm_session)
        {
            continue;
        }
        is_ntp_mode = npm_session->session_stats.is_ntp_ts;

        if (type == 0)/*AutoGenPktTxPktStats*/
        {
            sal_memset(tmp, 0, sizeof(tmp));
            dword = TABLE_ENTRY_OFFSET(lchip, AutoGenPktTxPktStats_t)>>2;
            p_addr_data =  p_addr + (dword)*session_id;
            g_npm_master[lchip]->tx_pkts[session_id] = GetAutoGenPktTxPktStats(V, txPkts_f, p_addr_data);

            npm_session->session_stats.tx_pkts_all += g_npm_master[lchip]->tx_pkts[session_id];

            GetAutoGenPktTxPktStats(A, txBytes_f, p_addr_data, tmp);
            npm_session->tx_bytes_all += (((uint64)tmp[1]<<32) | tmp[0]);
        }
        else if (type == 1)/*AutoGenPktRxPktStats*/
        {
            dword = TABLE_ENTRY_OFFSET(lchip, AutoGenPktRxPktStats_t)>>2;
            p_addr_data =  p_addr + dword*session_id;

            g_npm_master[lchip]->rx_pkts[session_id] = GetAutoGenPktRxPktStats(V, rxPkts_f, p_addr_data);
            npm_session->session_stats.rx_pkts_all += g_npm_master[lchip]->rx_pkts[session_id];
            sal_memset(tmp, 0, sizeof(tmp));
            GetAutoGenPktRxPktStats(A, rxBytes_f, p_addr_data, tmp);
            g_npm_master[lchip]->rx_bytes[session_id] = (((uint64)tmp[1]<<32) | tmp[0]);
            npm_session->rx_bytes_all += g_npm_master[lchip]->rx_bytes[session_id];

            /*NTP mode: low 32 bits uint: 1 / 2^32 ns, precision < 0.25 ns*/
            sal_memset(tmp, 0, sizeof(tmp));
            GetAutoGenPktRxPktStats(A, totalDTs_f , p_addr_data, tmp);
            total_delay = is_ntp_mode ? ((uint64)tmp[1]*1000000000 + (uint64)tmp[0]*SYS_NPM_DECIMAL_VALUE_FOR_PRECISION / 10000000000LLU) : (((uint64)tmp[1] << 32) + tmp[0]);
            npm_session->session_stats.total_delay_all += total_delay;

            sal_memset(tmp, 0, sizeof(tmp));
            GetAutoGenPktRxPktStats(A, totalDTs2_f , p_addr_data, tmp);
            total_far_delay = is_ntp_mode ? ((uint64)tmp[1]*1000000000 + (uint64)tmp[0]*SYS_NPM_DECIMAL_VALUE_FOR_PRECISION / 10000000000LLU) : (((uint64)tmp[1] << 32) + tmp[0]);
            npm_session->total_far_delay_all += total_far_delay;

            /*max jitter*/
            max_jitter = GetAutoGenPktRxPktStats(V, maxJitter_f, p_addr_data);
            max_jitter = is_ntp_mode ? ((uint64)max_jitter*SYS_NPM_DECIMAL_VALUE_FOR_PRECISION / 10000000000LLU) : max_jitter;
            npm_session->session_stats.max_jitter = (npm_session->session_stats.max_jitter < max_jitter) ? max_jitter : npm_session->session_stats.max_jitter;

            /*total jitter*/
            total_jitter = GetAutoGenPktRxPktStats(V, totalJitter_f, p_addr_data);
            total_jitter = is_ntp_mode ? ((uint64)total_jitter*SYS_NPM_DECIMAL_VALUE_FOR_PRECISION / 10000000000LLU) : total_jitter;
            npm_session->session_stats.total_jitter_all += total_jitter;

            /*min delay*/
            sal_memset(tmp, 0, sizeof(tmp));
            GetAutoGenPktRxPktStats(A, minDelay_f , p_addr_data, tmp);
            min_delay = is_ntp_mode ? ((uint64)tmp[1]*1000000000 + (uint64)tmp[0]*SYS_NPM_DECIMAL_VALUE_FOR_PRECISION / 10000000000LLU) : (((uint64)tmp[1] << 32) + tmp[0]);

            /*min jitter*/
            min_jitter = GetAutoGenPktRxPktStats(V, minJitter_f, p_addr_data);
            min_jitter = is_ntp_mode ? ((uint64)min_jitter*SYS_NPM_DECIMAL_VALUE_FOR_PRECISION / 10000000000LLU) : min_jitter;
            if (min_delay)/*min_delay=0, means stats has been cleared on read*/
            {
                npm_session->session_stats.min_delay = (npm_session->session_stats.min_delay > min_delay) ? min_delay : npm_session->session_stats.min_delay;
                npm_session->session_stats.min_jitter = (npm_session->session_stats.min_jitter > min_jitter) ? min_jitter : npm_session->session_stats.min_jitter;
            }

            /*max delay*/
            sal_memset(tmp, 0, sizeof(tmp));
            GetAutoGenPktRxPktStats(A, maxDelay_f, p_addr_data, tmp);
            max_delay = is_ntp_mode ? ((uint64)tmp[1]*1000000000 + (uint64)tmp[0]*SYS_NPM_DECIMAL_VALUE_FOR_PRECISION / 10000000000LLU) : (((uint64)tmp[1] << 32) + tmp[0]);
            npm_session->session_stats.max_delay = (npm_session->session_stats.max_delay < max_delay) ? max_delay : npm_session->session_stats.max_delay;

            /*total seq_disorder*/
            if (DRV_FROM_AT(lchip))
            {
                npm_session->session_stats.disorder_pkts += GetAutoGenPktRxPktStats(V, seqDisorderCnt_f, p_addr_data);
            }
        }
        else if (type == 2)/*AutoGenPktTxPktAck*/
        {
            if (!DRV_FROM_AT(lchip))
            {
                uint32 tmp_disorder_pkts = 0;
                dword = TABLE_ENTRY_OFFSET(lchip, AutoGenPktTxPktAck_t) >> 2;
                p_addr_data =  p_addr + dword*session_id;
                tmp_disorder_pkts = GetAutoGenPktTxPktAck(V, seqDisorderCnt_f, p_addr_data);
                npm_session->session_stats.disorder_pkts += (npm_session->last_disorder_pkts > tmp_disorder_pkts)? (tmp_disorder_pkts + 0x10000 - npm_session->last_disorder_pkts )
                : (tmp_disorder_pkts - npm_session->last_disorder_pkts);
                npm_session->last_disorder_pkts = tmp_disorder_pkts;
            }
            sal_memset(tmp, 0, sizeof(tmp));
            GetAutoGenPktTxPktAck(A, firstTs_f , p_addr_data, &tmp);
            first_ts = is_ntp_mode ? ((uint64)tmp[1]*1000000000 + (uint64)tmp[0]*SYS_NPM_DECIMAL_VALUE_FOR_PRECISION / 10000000000LLU) : ((uint64)tmp[1]*1000000000 + tmp[0]);
            time_stamp = p_ext_data->time_stamp;
            SYS_NPM_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "first_ts:%"PRIu64" s, last_ts:%"PRIu64" s\n", first_ts/1000000000, time_stamp/1000000000);
            if ((time_stamp > first_ts) && (((time_stamp - first_ts) / 1000000000) < 1))
            {
                is_first = 1;
            }
        }

        /*SES judgement*/
        if (type != 2)
        {
            continue;
        }

        cmd = DRV_IOR(AutoGenPktEn_t, DRV_ENTRY_FLAG);
        CTC_ERROR_RETURN_WITH_UNLOCK(DRV_IOCTL(lchip, session_id, DRV_CMD_PP_EN(cmd), &autogen_en), g_npm_master[lchip]->npm_mutex);
        auto_en = GetAutoGenPktEn(V, autoGenEn_f, &autogen_en);
        if (g_npm_master[lchip]->tx_pkts[session_id])
        {
            lost_rate = (g_npm_master[lchip]->tx_pkts[session_id] - g_npm_master[lchip]->rx_pkts[session_id]) *100 / (g_npm_master[lchip]->tx_pkts[session_id]) + (((g_npm_master[lchip]->tx_pkts[session_id] - g_npm_master[lchip]->rx_pkts[session_id])*100  % (g_npm_master[lchip]->tx_pkts[session_id]))? 1 : 0);

            if (lost_rate >= (npm_session->flr_for_ses))
            {
                is_ses = 1;
                npm_session->ses_cnt++;
            }
            else
            {
                is_ses = 0;
                npm_session->non_ses_cnt++;
            }
        }
        if (auto_en)
        {
            /*available period judgement*/
            if (npm_session->in_avl_period == is_ses)
            {
                is_ses? (npm_session->continuous_ses_cnt++) : (npm_session->continuous_non_ses_cnt++);
                if ((npm_session->continuous_ses_cnt >= npm_session->avl_period_thrd)
                    || (npm_session->continuous_non_ses_cnt >= npm_session->avl_period_thrd))
                {
                    npm_session->in_avl_period = !is_ses;
                    is_ses? (npm_session->continuous_ses_cnt = 0) : (npm_session->continuous_non_ses_cnt = 0);

                    npm_event.event_entry[npm_event.valid_num].in_avl_period =  !is_ses;
                    npm_event.event_entry[npm_event.valid_num].session_id = session_id;
                    npm_event.event_entry[npm_event.valid_num].seconds = npm_session->seconds;
                    npm_event.event_entry[npm_event.valid_num].avl_periods[0] = npm_session->avl_periods;
                    npm_event.event_entry[npm_event.valid_num].avl_periods[1] = npm_session->unavl_periods;
                    npm_event.event_entry[npm_event.valid_num].ir[0] = npm_session->max_rx_ir;
                    npm_event.event_entry[npm_event.valid_num].ir[1] = npm_session->min_rx_ir;
                    npm_event.valid_num++;
                    if (npm_event.valid_num >= CTC_NPM_EVENT_MAX_NUM )
                    {
                        npm_cb(gchip, &npm_event);
                        npm_event.valid_num = 0;
                    }
                }
            }
            else
            {
                is_ses? (npm_session->continuous_non_ses_cnt = 0) : (npm_session->continuous_ses_cnt = 0);
            }

            npm_session->in_avl_period? (npm_session->avl_periods++) : (npm_session->unavl_periods++);
            ir = g_npm_master[lchip]->rx_bytes[session_id];
            npm_session->max_rx_ir = (ir >= npm_session->max_rx_ir)? ir : npm_session->max_rx_ir;
            npm_session->min_rx_ir = (ir <= npm_session->min_rx_ir && !is_first)? ir : npm_session->min_rx_ir;
            SYS_NPM_DBG_OUT(CTC_DEBUG_LEVEL_INFO, "is_first:%d, ir:%"PRIu64"s\n", is_first, ir);
            npm_session->seconds ++;
        }
    }
    if (type == 2)
    {
        if (npm_event.valid_num)
        {
            npm_cb(gchip, &npm_event);
        }
    }

    NPM_UNLOCK(lchip);
    return ret;
    /* 
    if type == 3 get twamp lite(table DsOamTwampStats) stats execute npm lite seesion process 
    else get autogen stats
    */
NPM_LITE_SESSION_PROC:
    if (DRV_FROM_AT(lchip))
    {
        NPM_UNLOCK(lchip);
        return ret;
    }

    for (session_id = 0; session_id < SYS_NPM_MAX_TWAMP_LITE_SESSION_NUM ; session_id++)
    {
        if (!g_npm_master[lchip]->session_lite->used_cnt)
        {
            break;
        }
        npm_lite_session = (sys_npm_lite_session_t*)ctc_vector_get(g_npm_master[lchip]->session_lite, session_id);
        if (!npm_lite_session)
        {
            continue;
        }
        is_ntp_mode = npm_lite_session->ts_format;
        dword = TABLE_ENTRY_OFFSET(lchip, AutoGenPktRxPktStats_t)>>2;
        p_addr_data =  p_addr + dword*(npm_lite_session->twamp_idx);

        rx_pkts_all = GetDsOamTwampStats(V, rxPkts_f, p_addr_data);
        npm_lite_session->session_stats.rx_pkts_all += (rx_pkts_all >= npm_lite_session->session_old_stats.rx_pkts_all) ? 
            (rx_pkts_all - npm_lite_session->session_old_stats.rx_pkts_all) : (SYS_NPM_UINT16_MAX + 1 - npm_lite_session->session_old_stats.rx_pkts_all + rx_pkts_all);
        npm_lite_session->session_old_stats.rx_pkts_all = rx_pkts_all;

        rx_fcl_all = GetDsOamTwampStats(V, rxFcl_f, p_addr_data);
        npm_lite_session->lite_stats.rx_fcl_all += (rx_fcl_all >= npm_lite_session->lite_old_stats.rx_fcl_all) ? 
            (rx_fcl_all - npm_lite_session->lite_old_stats.rx_fcl_all) : (SYS_NPM_UINT16_MAX + 1 - npm_lite_session->lite_old_stats.rx_fcl_all + rx_fcl_all);
        npm_lite_session->lite_old_stats.rx_fcl_all = rx_fcl_all;

        tx_pkts_all = GetDsOamTwampStats(V, txFcb_f, p_addr_data);
        npm_lite_session->session_stats.tx_pkts_all += (tx_pkts_all >= npm_lite_session->session_old_stats.tx_pkts_all) ? 
            (tx_pkts_all - npm_lite_session->session_old_stats.tx_pkts_all) : (SYS_NPM_UINT16_MAX + 1 - npm_lite_session->session_old_stats.tx_pkts_all + tx_pkts_all);
        npm_lite_session->session_old_stats.tx_pkts_all = tx_pkts_all;
        
        disorder_pkts = GetDsOamTwampStats(V, seqDisorderCnt_f, p_addr_data);
        npm_lite_session->session_stats.disorder_pkts += (disorder_pkts >= npm_lite_session->session_old_stats.disorder_pkts) ? 
            (disorder_pkts - npm_lite_session->session_old_stats.disorder_pkts) : (SYS_NPM_UINT10_MAX + 1 - npm_lite_session->session_old_stats.disorder_pkts + disorder_pkts);
        npm_lite_session->session_old_stats.disorder_pkts = disorder_pkts;

        total_delay = GetDsOamTwampStats(V, totalDTs_f , p_addr_data);
        total_delay = is_ntp_mode ? (((total_delay & 0x38000000) >> 27)*1000000000 + ((total_delay &0x7FFFFFF) << 5)*SYS_NPM_DECIMAL_VALUE_FOR_PRECISION / 10000000000LLU) : (((total_delay & 0x38000000) << 5) + ((total_delay & 0x7FFFFFF) << 5));
        max_total_delay = is_ntp_mode ? SYS_NPM_NTP_MAX_TOTAL_DELAY : SYS_NPM_PTP_MAX_TOTAL_DELAY;
        npm_lite_session->session_stats.total_delay_all += (total_delay >= npm_lite_session->session_old_stats.total_delay_all) ?
            (total_delay - npm_lite_session->session_old_stats.total_delay_all) : (max_total_delay + 1 - npm_lite_session->session_old_stats.total_delay_all + total_delay);
        npm_lite_session->session_old_stats.total_delay_all = total_delay;

        max_jitter = SYS_NPM_TS_TO_NS(is_ntp_mode, GetDsOamTwampStats(V, array_2_maxJitter_f, p_addr_data));
        npm_lite_session->lite_stats.two_way_max_jitter = (npm_lite_session->lite_stats.two_way_max_jitter < max_jitter)? max_jitter : npm_lite_session->lite_stats.two_way_max_jitter;

        max_jitter =SYS_NPM_TS_TO_NS(is_ntp_mode, GetDsOamTwampStats(V, array_1_maxJitter_f, p_addr_data));
        npm_lite_session->lite_stats.far_max_jitter = (npm_lite_session->lite_stats.far_max_jitter < max_jitter)? max_jitter : npm_lite_session->lite_stats.far_max_jitter;

        max_jitter = SYS_NPM_TS_TO_NS(is_ntp_mode, GetDsOamTwampStats(V, array_0_maxJitter_f, p_addr_data));
        npm_lite_session->lite_stats.near_max_jitter = (npm_lite_session->lite_stats.near_max_jitter < max_jitter)? max_jitter : npm_lite_session->lite_stats.near_max_jitter;

        two_way_total_jitter_all = SYS_NPM_TS_TO_NS(is_ntp_mode, GetDsOamTwampStats(V, array_2_totalJitter_f, p_addr_data));
        npm_lite_session->lite_stats.two_way_total_jitter_all += (two_way_total_jitter_all >= npm_lite_session->lite_old_stats.two_way_total_jitter_all)?
            (two_way_total_jitter_all - npm_lite_session->lite_old_stats.two_way_total_jitter_all) : 
            (SYS_NPM_UINT22_MAX + 1 - npm_lite_session->lite_old_stats.two_way_total_jitter_all + two_way_total_jitter_all);
        npm_lite_session->lite_old_stats.two_way_total_jitter_all = two_way_total_jitter_all;
        
        near_total_jitter_all = SYS_NPM_TS_TO_NS(is_ntp_mode, GetDsOamTwampStats(V, array_0_totalJitter_f, p_addr_data));
        npm_lite_session->lite_stats.near_total_jitter_all += (near_total_jitter_all >= npm_lite_session->lite_old_stats.near_total_jitter_all)?
            (near_total_jitter_all - npm_lite_session->lite_old_stats.near_total_jitter_all) : 
            (SYS_NPM_UINT22_MAX + 1 - npm_lite_session->lite_old_stats.near_total_jitter_all + near_total_jitter_all);
        npm_lite_session->lite_old_stats.near_total_jitter_all = near_total_jitter_all;

        far_total_jitter_all = SYS_NPM_TS_TO_NS(is_ntp_mode, GetDsOamTwampStats(V, array_1_totalJitter_f, p_addr_data));
        npm_lite_session->lite_stats.far_total_jitter_all += (far_total_jitter_all >= npm_lite_session->lite_old_stats.far_total_jitter_all)?
            (far_total_jitter_all - npm_lite_session->lite_old_stats.far_total_jitter_all) : 
            (SYS_NPM_UINT22_MAX + 1 - npm_lite_session->lite_old_stats.far_total_jitter_all + far_total_jitter_all);
        npm_lite_session->lite_old_stats.far_total_jitter_all = far_total_jitter_all;
        
        min_delay = GetDsOamTwampStats(V, minDelay_f , p_addr_data);
        min_delay = is_ntp_mode ? (((min_delay&0x38000000) >> 27)*1000000000 + ((min_delay & 0x7FFFFFF) << 5)*SYS_NPM_DECIMAL_VALUE_FOR_PRECISION / 10000000000LLU) : (((min_delay & 0x38000000) << 5) + ((min_delay & 0x7FFFFFF) << 5));

        two_way_min_jitter = SYS_NPM_TS_TO_NS(is_ntp_mode, GetDsOamTwampStats(V, array_2_minJitter_f, p_addr_data));
        far_min_jitter =SYS_NPM_TS_TO_NS(is_ntp_mode, GetDsOamTwampStats(V, array_1_minJitter_f, p_addr_data));
        near_min_jitter =SYS_NPM_TS_TO_NS(is_ntp_mode, GetDsOamTwampStats(V, array_0_minJitter_f, p_addr_data));
        if (min_delay)/*min_delay=0, means stats has been cleared on read*/
        {
            npm_lite_session->session_stats.min_delay = (npm_lite_session->session_stats.min_delay > min_delay)? min_delay : npm_lite_session->session_stats.min_delay;
            npm_lite_session->lite_stats.two_way_min_jitter = (npm_lite_session->lite_stats.two_way_min_jitter > two_way_min_jitter)? two_way_min_jitter : npm_lite_session->lite_stats.two_way_min_jitter;
            npm_lite_session->lite_stats.far_min_jitter = (npm_lite_session->lite_stats.far_min_jitter > far_min_jitter)? far_min_jitter : npm_lite_session->lite_stats.far_min_jitter;
            npm_lite_session->lite_stats.near_min_jitter = (npm_lite_session->lite_stats.near_min_jitter > near_min_jitter)? near_min_jitter : npm_lite_session->lite_stats.near_min_jitter;
        }
        max_delay = GetDsOamTwampStats(V, maxDelay_f , p_addr_data);
        max_delay = is_ntp_mode ? (((max_delay & 0x38000000) >> 27)*1000000000 + ((max_delay & 0x7FFFFFF) << 5)*SYS_NPM_DECIMAL_VALUE_FOR_PRECISION / 10000000000LLU) : (((max_delay & 0x38000000) << 5) + ((max_delay&0x7FFFFFF) << 5));
        npm_lite_session->session_stats.max_delay = (npm_lite_session->session_stats.max_delay < max_delay)? max_delay : npm_lite_session->session_stats.max_delay;
    }

    NPM_UNLOCK(lchip);
    return ret;
}
#endif
#endif
